package com.living.player.utils;

import android.os.Handler;

import com.living.player.widget.media.IjkVideoView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * CopyRright (c),2013-2023,YunXueTang Co. Ltd <br>
 * Project:Course <br>
 * Author: wanggq <br>
 * Create Date: 2018/3/19 <br>
 * Mail:wanggq@yxt.com  <br>
 * Comments: 播放隐藏定时器优化   <br>
 * Modified By:wanggaoqiang <br>
 * Modified Date: 2018/3/19 <br>
 * Why & What is modified:添加注释 <br>
 * Version:5.6 <br>
 */
public class YXTPlayerProgressTimerTask implements Runnable {

    IYXTPlayerProgressTask iyxtPlayerProgressTask;
    Handler mainThreadHandler;
    private boolean isMaxSetted = false;  // 是否设置过最大进度
    boolean isDraggingProgress;  // 标记进度正在改变
    private Timer positionTimer;
    private static final int POSITION_REFRESH_TIME = 1000; // 1秒 , 更新一次
    /**
     * 学习时间
     */
    protected int learnTime = 0;
    private IjkVideoView bVideoView;
    private int currentPositionInMilliSeconds = 0;

    public void setDraggingProgress(boolean draggingProgress) {
        isDraggingProgress = draggingProgress;
    }

    public int getLearnTime() {
        return learnTime;
    }

    public int getCurrentPositionInMilliSeconds() {
        return currentPositionInMilliSeconds;
    }

    public YXTPlayerProgressTimerTask(Handler mainThreadHandler, IjkVideoView bVideoView, IYXTPlayerProgressTask iyxtPlayerProgressTask) {
        this.mainThreadHandler = mainThreadHandler;
        this.bVideoView = bVideoView;
        this.iyxtPlayerProgressTask = iyxtPlayerProgressTask;
    }

    /**
     * 初始化学习
     */
    public void initLearnTime() {
        learnTime = 0;
    }

    /**
     * 开始更新进度
     */
    public void startPositionTimer() {
        if (positionTimer != null) {
            positionTimer.cancel();
            positionTimer = null;
        }
        // Log.e("--onPositionUpdate-startPositionTimer-：" + bVideoView.getCurrentPosition());
        // onPositionUpdate();
        positionTimer = new Timer();
        positionTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mainThreadHandler.post(YXTPlayerProgressTimerTask.this);
            }
        }, 0, POSITION_REFRESH_TIME);
    }

    /**
     * 结束更新进度
     */
    public void stopPositionTimer() {
        if (positionTimer != null) {
            positionTimer.cancel();
            positionTimer = null;
        }
        mainThreadHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void run() {
        if (bVideoView != null && bVideoView.isPlaying()) {
            if (learnTime < 1024 * 1024) {
                learnTime++;
            }
        }
        onPositionUpdate();
    }

    /**
     * 更新进度
     * onPositionUpdate is invoked per 500ms
     */
    public boolean onPositionUpdate() {
        if (bVideoView == null) {
            return false;
        }
        if (isDraggingProgress && bVideoView.isPlaying()) return false;
        if (bVideoView.isPlaying()) {
            int newPositionInMilliSeconds = bVideoView.getCurrentPosition();
//            Log.e("--onPositionUpdate--：" + newPositionInMilliSeconds);
            long previousPosition = currentPositionInMilliSeconds;
            if (newPositionInMilliSeconds >= 0) {
                currentPositionInMilliSeconds = newPositionInMilliSeconds;
            }
            int durationInMilliSeconds = bVideoView.getDuration();
            if (durationInMilliSeconds > 0) {
                this.setMax(durationInMilliSeconds);
                // 直播视频的duration为0，此时不设置进度
                if (previousPosition != newPositionInMilliSeconds) {
                    iyxtPlayerProgressTask.onTimePositionUpdate(newPositionInMilliSeconds);
                }
            }
        }
        return false;
    }

    /***
     * 设置最大进度
     * @param maxProgress
     */
    private void setMax(int maxProgress) {
        if (isMaxSetted) {
            return;
        }
        iyxtPlayerProgressTask.updateDuration(maxProgress);
        if (maxProgress > 0) {
            isMaxSetted = true;
        }
    }


}
